package com.mini.service;

import com.mini.model.BusinessChanceUserProfile;
import com.mini.model.BusinessUserProfileDic;
import com.mini.repository.BusinessChanceUserProfileRepository;
import com.mini.repository.BusinessUserProfileDicRepository;
import com.mini.vo.ChanceUserProfileAddVO;
import com.mini.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ChanceUserProfileService {


    @Autowired
    private BusinessUserProfileDicRepository userProfileDicRepository;
    @Autowired
    private BusinessChanceUserProfileRepository chanceUserProfileRepository;


    /**
     * 画像字典-前端使用的map
     * @return
     */
    public Map<String, List<BusinessUserProfileDic>> dicShowMap(){
        Map<String, List<BusinessUserProfileDic>> map = new HashMap<>();

        List<BusinessUserProfileDic> list = this.dicShowList();
        if(CollectionUtils.isEmpty(list)){
            return map;
        }

        map = list.stream().filter(o-> StringUtils.isNotBlank(o.getProfileType()))
                .collect(Collectors.groupingBy(BusinessUserProfileDic::getProfileType));

        for(Map.Entry<String, List<BusinessUserProfileDic>> entity : map.entrySet()){
            List<BusinessUserProfileDic> value = entity.getValue();
            //排序
            if(value != null){
                value.sort(Comparator.comparing(BusinessUserProfileDic::getShowOrder));
            }

        }

        return map;
    }

    /**
     * 画像字典-可展示的全部列表
     * @return
     */
    public List<BusinessUserProfileDic> dicShowList(){

        return userProfileDicRepository.findByShowStateOrderByShowOrderDesc(1);

    }


    public BusinessChanceUserProfile save(ChanceUserProfileAddVO param, UserVO operateUser){
        if(param == null || operateUser == null
                || param.getChanceId() == null
                || param.getProfileType() == null
                || param.getProfileId() == null
                || StringUtils.isBlank(param.getProfileName())){
            throw new IllegalArgumentException("缺少参数！");
        }

        Long chanceId = param.getChanceId();
        String profileType = param.getProfileType();

        BusinessChanceUserProfile record = this.chanceUserProfileRepository.findFirstByChanceIdAndProfileType(chanceId, profileType);
        if(record != null){
            record.setUpdateUserId(operateUser.getId());
            record.setUpdateTime(new Date());
        }else {
            record = new BusinessChanceUserProfile();
            record.setCreateUserId(operateUser.getId());
            record.setCreateTime(new Date());
            record.setUpdateUserId(operateUser.getId());
            record.setUpdateTime(new Date());
        }
        record.setChanceId(chanceId);
        record.setProfileType(profileType);
        record.setProfileId(param.getProfileId());
        record.setProfileName(param.getProfileName());

        return this.chanceUserProfileRepository.save(record);
    }


    public Map<String, BusinessChanceUserProfile> getUserProfile(Long chanceId){
        Map<String, BusinessChanceUserProfile> map = new HashMap();

        List<BusinessChanceUserProfile> userProfiles = this.chanceUserProfileRepository.findByChanceId(chanceId);
        if(CollectionUtils.isEmpty(userProfiles)){
            return map;
        }

        map = userProfiles.stream().collect(Collectors.toMap(BusinessChanceUserProfile::getProfileType, o->o, (o1,o2)->{

            return o2;
        }));

        return map;

    }

    public Map<Long, Map<String, BusinessChanceUserProfile>> getUserProfileMap(Collection<Long> chanceIds){
        Map<Long, Map<String, BusinessChanceUserProfile>> map = new HashMap<>();
        if(CollectionUtils.isEmpty(chanceIds)){
            return map;
        }

        List<BusinessChanceUserProfile> list = this.chanceUserProfileRepository.findByChanceIdIn(new HashSet(chanceIds));
        if(CollectionUtils.isEmpty(list)){
            return map;
        }

        Map<Long, List<BusinessChanceUserProfile>> userMap = list.stream().collect(Collectors.groupingBy(BusinessChanceUserProfile :: getChanceId));

        for(Map.Entry<Long, List<BusinessChanceUserProfile>> entry : userMap.entrySet()){
            Long chanceId = entry.getKey();
            List<BusinessChanceUserProfile> userProfiles = entry.getValue();
            if(CollectionUtils.isEmpty(userProfiles)){
                continue;
            }
            Map<String, BusinessChanceUserProfile> userProfileMap = userProfiles.stream().collect(Collectors.toMap(BusinessChanceUserProfile::getProfileType, o->o, (o1,o2)->{
                return o2;
            }));

            map.put(chanceId, userProfileMap);

        }

        return map;
    }





}
